home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlcpio.zip / FILEMODE.C < prev    next >
C/C++ Source or Header  |  1990-06-29  |  5KB  |  200 lines

  1. /* filemode.c -- make a string describing file modes
  2.    Copyright (C) 1985, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #ifdef _POSIX_SOURCE
  21. #define S_IREAD S_IRUSR
  22. #define S_IWRITE S_IWUSR
  23. #define S_IEXEC S_IXUSR
  24. #endif
  25.  
  26. void mode_string ();
  27. static char ftypelet ();
  28. static void rwx ();
  29. static void setst ();
  30.  
  31. /* filemodestring - fill in string STR with an ls-style ASCII
  32.    representation of the st_mode field of file stats block STATP.
  33.    10 characters are stored in STR; no terminating null is added.
  34.    The characters stored in STR are:
  35.  
  36.    0    File type.  'd' for directory, 'c' for character
  37.     special, 'b' for block special, 'm' for multiplex,
  38.     'l' for symbolic link, 's' for socket, 'p' for fifo,
  39.     '-' for regular, '?' for any other file type
  40.  
  41.    1    'r' if the owner may read, '-' otherwise.
  42.  
  43.    2    'w' if the owner may write, '-' otherwise.
  44.  
  45.    3    'x' if the owner may execute, 's' if the file is
  46.     set-user-id, '-' otherwise.
  47.     'S' if the file is set-user-id, but the execute
  48.     bit isn't set.
  49.  
  50.    4    'r' if group members may read, '-' otherwise.
  51.  
  52.    5    'w' if group members may write, '-' otherwise.
  53.  
  54.    6    'x' if group members may execute, 's' if the file is
  55.     set-group-id, '-' otherwise.
  56.     'S' if it is set-group-id but not executable.
  57.  
  58.    7    'r' if any user may read, '-' otherwise.
  59.  
  60.    8    'w' if any user may write, '-' otherwise.
  61.  
  62.    9    'x' if any user may execute, 't' if the file is "sticky"
  63.     (will be retained in swap space after execution), '-'
  64.     otherwise.
  65.     'T' if the file is sticky but not executable. */
  66.  
  67. void
  68. filemodestring (statp, str)
  69.      struct stat *statp;
  70.      char *str;
  71. {
  72.   mode_string (statp->st_mode, str);
  73. }
  74.  
  75. /* Like filemodestring, but only the relevant part of the `struct stat'
  76.    is given as an argument. */
  77.  
  78. void
  79. mode_string (mode, str)
  80.      unsigned short mode;
  81.      char *str;
  82. {
  83.   str[0] = ftypelet (mode);
  84.   rwx ((mode & 0700) << 0, &str[1]);
  85.   rwx ((mode & 0070) << 3, &str[4]);
  86.   rwx ((mode & 0007) << 6, &str[7]);
  87.   setst (mode, str);
  88. }
  89.  
  90. /* Return a character indicating the type of file described by
  91.    file mode BITS:
  92.    'd' for directories
  93.    'b' for block special files
  94.    'c' for character special files
  95.    'm' for multiplexor files
  96.    'l' for symbolic links
  97.    's' for sockets
  98.    'p' for fifos
  99.    '-' for regular files
  100.    '?' for any other file type. */
  101.  
  102. static char
  103. ftypelet (bits)
  104.      unsigned short bits;
  105. {
  106.   switch (bits & S_IFMT)
  107.     {
  108.     default:
  109.       return '?';
  110.     case S_IFREG:
  111.       return '-';
  112.     case S_IFDIR:
  113.       return 'd';
  114. #ifdef S_IFLNK
  115.     case S_IFLNK:
  116.       return 'l';
  117. #endif
  118. #ifdef S_IFCHR
  119.     case S_IFCHR:
  120.       return 'c';
  121. #endif
  122. #ifdef S_IFBLK
  123.     case S_IFBLK:
  124.       return 'b';
  125. #endif
  126. #ifdef S_IFMPC
  127.     case S_IFMPC:
  128.     case S_IFMPB:
  129.       return 'm';
  130. #endif
  131. #ifdef S_IFSOCK
  132.     case S_IFSOCK:
  133.       return 's';
  134. #endif
  135. #ifdef S_IFIFO
  136. #if S_IFIFO != S_IFSOCK
  137.     case S_IFIFO:
  138.       return 'p';
  139. #endif
  140. #endif
  141. #ifdef S_IFNWK            /* HP-UX */
  142.     case S_IFNWK:
  143.       return 'n';
  144. #endif
  145.     }
  146. }
  147.  
  148. /* Look at read, write, and execute bits in BITS and set
  149.    flags in CHARS accordingly. */
  150.  
  151. static void
  152. rwx (bits, chars)
  153.      unsigned short bits;
  154.      char *chars;
  155. {
  156.   chars[0] = (bits & S_IREAD) ? 'r' : '-';
  157.   chars[1] = (bits & S_IWRITE) ? 'w' : '-';
  158.   chars[2] = (bits & S_IEXEC) ? 'x' : '-';
  159. }
  160.  
  161. /* Set the 's' and 't' flags in file attributes string CHARS,
  162.    according to the file mode BITS. */
  163.  
  164. static void
  165. setst (bits, chars)
  166.      unsigned short bits;
  167.      char *chars;
  168. {
  169. #ifdef S_ISUID
  170.   if (bits & S_ISUID)
  171.     {
  172.       if (chars[3] != 'x')
  173.     /* Set-uid, but not executable by owner. */
  174.     chars[3] = 'S';
  175.       else
  176.     chars[3] = 's';
  177.     }
  178. #endif
  179. #ifdef S_ISGID
  180.   if (bits & S_ISGID)
  181.     {
  182.       if (chars[6] != 'x')
  183.     /* Set-gid, but not executable by group. */
  184.     chars[6] = 'S';
  185.       else
  186.     chars[6] = 's';
  187.     }
  188. #endif
  189. #ifdef S_ISVTX
  190.   if (bits & S_ISVTX)
  191.     {
  192.       if (chars[9] != 'x')
  193.     /* Sticky, but not executable by others. */
  194.     chars[9] = 'T';
  195.       else
  196.     chars[9] = 't';
  197.     }
  198. #endif
  199. }
  200.